﻿// NotificationForm.cs
using System;
using System.Drawing;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ClipboardUrlMonitor
{
    public partial class NotificationForm : Form
    {
        private readonly string _url;
        private readonly string _title;
        private readonly string _imageUrl;
        private readonly string _summary;

        public NotificationForm(string url, string title, string imageUrl, string summary)
        {
            InitializeComponent();
            _url = url;
            _title = title;
            _imageUrl = imageUrl;
            _summary = summary;

            // 初始化窗体样式
            this.StartPosition = FormStartPosition.CenterScreen;
            this.TopMost = true;
            this.ShowInTaskbar = false;
        }

        private async void NotificationForm_Load(object sender, EventArgs e)
        {
            // 设置基础信息
            lblTitle.Text = _title;
            txtUrl.Text = _url;
            this.txtSummary.Text = _summary;
            // 异步加载图片
            await LoadPreviewImageAsync();

            // 自动关闭计时
            StartAutoCloseTimer();
        }

        private async Task LoadPreviewImageAsync()
        {
            try
            {
                using var httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(5) };

                var imageBytes = await httpClient.GetByteArrayAsync(_imageUrl);
                using (var stream = new System.IO.MemoryStream(imageBytes))
                {
                    var image = Image.FromStream(stream);
                    picPreview.Image = new Bitmap(image);
                }
            }
            catch
            {
                Image img = null;
                var di = Properties.Resources.DefaultImage;
                using (var stream = new System.IO.MemoryStream(di))
                {
                    var image = Image.FromStream(stream);
                    img = new Bitmap(image);
                }
                picPreview.Image = img;//Properties.Resources.DefaultImage; // 需添加默认图片资源
                picPreview.SizeMode = PictureBoxSizeMode.CenterImage;
            }
        }



        private void StartAutoCloseTimer()
        {
            var timer = new System.Windows.Forms.Timer { Interval = 5000 };
            timer.Tick += (s, e) =>
            {
                timer.Stop();
                //this.Close();
            };
            timer.Start();
        }

        #region Windows Form Designer generated code

        private Label lblTitle;
        private TextBox txtUrl;
        private PictureBox picPreview;
        private Button btnClose;
        private TextBox txtSummary;
        private void InitializeComponent()
        {
            this.lblTitle = new Label();
            this.txtUrl = new TextBox();
            this.picPreview = new PictureBox();
            this.btnClose = new Button();
            this.txtSummary = new TextBox();
            ((System.ComponentModel.ISupportInitialize)(this.picPreview)).BeginInit();
            this.SuspendLayout();

            // 
            // lblTitle
            // 
            this.lblTitle.Font = new Font("微软雅黑", 12F, FontStyle.Bold);
            this.lblTitle.Location = new Point(15, 15);
            this.lblTitle.Margin = new Padding(4, 0, 4, 0);
            this.lblTitle.Name = "lblTitle";
            this.lblTitle.Size = new Size(450, 60);
            this.lblTitle.TabIndex = 0;
            this.lblTitle.TextAlign = ContentAlignment.MiddleCenter;

            // 
            // txtUrl
            // 
            this.txtUrl.BackColor = Color.White;
            this.txtUrl.BorderStyle = BorderStyle.FixedSingle;
            this.txtUrl.Font = new Font("Consolas", 9F);
            this.txtUrl.Location = new Point(15, 85);
            this.txtUrl.Multiline = true;
            this.txtUrl.Name = "txtUrl";
            this.txtUrl.ReadOnly = true;
            this.txtUrl.ScrollBars = ScrollBars.Vertical;
            this.txtUrl.Size = new Size(450, 60);
            this.txtUrl.TabIndex = 1;

            // 
            // picPreview
            // 
            this.picPreview.BackColor = SystemColors.ControlDark;
            this.picPreview.BorderStyle = BorderStyle.FixedSingle;
            this.picPreview.Location = new Point(15, 160);
            this.picPreview.Name = "picPreview";
            this.picPreview.Padding = new Padding(5);
            this.picPreview.Size = new Size(450, 250);
            this.picPreview.SizeMode = PictureBoxSizeMode.Zoom;
            this.picPreview.TabIndex = 2;
            this.picPreview.TabStop = false;

            // 
            // txtSummary
            // 
            this.txtSummary.BackColor = Color.White;
            this.txtSummary.BorderStyle = BorderStyle.FixedSingle;
            this.txtSummary.Font = new Font("Consolas", 9F);
            this.txtSummary.Location = new Point(15, 435);
            this.txtSummary.Multiline = true;
            this.txtSummary.Name = "txtSummary";
            this.txtSummary.ReadOnly = true;
            this.txtSummary.ScrollBars = ScrollBars.Vertical;
            this.txtSummary.Size = new Size(450, 200);
            this.txtSummary.TabIndex = 1;

            // 
            // btnClose
            // 
            this.btnClose.BackColor = Color.FromArgb(0, 120, 212);
            this.btnClose.FlatAppearance.BorderSize = 0;
            this.btnClose.FlatStyle = FlatStyle.Flat;
            this.btnClose.Font = new Font("微软雅黑", 9F);
            this.btnClose.ForeColor = Color.White;
            this.btnClose.Location = new Point(370, 680);
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new Size(95, 30);
            this.btnClose.TabIndex = 3;
            this.btnClose.Text = "关闭";
            this.btnClose.UseVisualStyleBackColor = false;
            this.btnClose.Click += (s, e) => this.Close();

            // 
            // NotificationForm
            // 
            this.AutoScaleDimensions = new SizeF(7F, 17F);
            this.AutoScaleMode = AutoScaleMode.Font;
            this.BackColor = Color.White;
            this.ClientSize = new Size(480, 800);
            this.Controls.Add(this.btnClose);
            this.Controls.Add(this.picPreview);
            this.Controls.Add(this.txtUrl);
            this.Controls.Add(this.lblTitle);
            this.Controls.Add(this.txtSummary);
            this.Font = new Font("微软雅黑", 9F);
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "NotificationForm";
            this.ShowIcon = false;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "网页内容提示";
            this.Load += new EventHandler(this.NotificationForm_Load);
            ((System.ComponentModel.ISupportInitialize)(this.picPreview)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        #endregion
    }
}